<?php
	class Trial extends CI_Controller
	{
		function __construct()
		{
			parent::__construct();
			$this->load->model('card_model');
			$this->load->helper('url');
			$this->load->library('session');
		} //end __construct
		
		function index()
		{
			$this->home();
		}

		public function home()
		{
			$this->load->view('trial/home_view');
		}
		
		
		public function start_game()
		{
			//On Click of Play
			$this->set_session();
			$deck = $this->get_deck();
			$table = $this->session->userdata('table');
			$this->session->set_userdata('deck', $deck);
			$hand1 = [];
			$hand2 = [];

			//$this->set_hand($hand1, 1);
			$hand1 = $this->get_hand(1);

			//$this->set_hand($hand2, 2);
			$hand2 = $this->get_hand(2);

			$trump = $this->get_trump();
			$this->session->set_userdata('trump', $trump);
			$this->session->set_userdata('table', $table);
			$player['first'] = $hand1;
			$player['second'] = $hand2;
			$player['table'] = $table;
			$player['trump'] = $trump;

			$this->load->view('trial/p1_view', $player);		
		}
//--------------------------------------------------------------------------//
//  							GET FUNCTIONS 								//
//--------------------------------------------------------------------------//
		function get_deck()
		{
			$deck = $this->card_model->get_deck();
			shuffle($deck);
			return $deck;
		} //end get_deck

		function get_hand($player)
		{
			$hand = $this->card_model->get_hand($player);
			return $hand;
		} //end get_hand

		function get_trump()
		{
			$deck = $this->session->userdata('deck');
			$trump = array_pop($deck);
			array_unshift($deck, $trump);
			$this->session->set_userdata('deck');
		} //end get_trump
		
//--------------------------------------------------------------------------//
// 								SET FUNCTIONS 						        //
//--------------------------------------------------------------------------//	
		function set_session()
		{
			$session_data = array(
					'deck' => array(),
					'table' => array(),
					'trump' => array(),
					'player' => 0,
					'p1_score' => 0,
					'p2_score' => 0
			);
			$this->session->set_userdata($session_data);
		} //end set_session

		function set_hand($hand, $player)
		{
			$deck = $this->session->userdata('deck');
			$count = count($hand);

			for($i = $count; $i < 6; $i++)
			{
				$card = array_pop($deck);
				array_push($hand, $card);
			}

			foreach($hand as $card):
				$this->card_model->set_hand($player, $card['suit_id'], $card['face']);
			endforeach;
			$this->session->set_userdata('deck', $deck);
		} //end set_hand

		function set_attack($turn, $index)
		{
			$table = $this->session->userdata('table');
			var_dump($table);
			$turn = intval($turn);
			$hand = $this->get_hand($turn);
			$attack = $hand[$index];
			if(count($table) == 0) //check if table has contents
			{						//if true, allow any card to attack
				
				unset($hand[$index]);
				$hand = array_values($hand);
				array_push($table, $attack);
				$this->card_model->remove_from($turn, $attack['suit_id'], $attack['face']);
			}
			else
			{
				foreach($table as $card):
					if($card['face'] == $attack['face'])
					{
						unset($hand[$index]);
						$hand = array_values($hand);
						array_push($table, $attack);
						$this->card_model->remove_from($turn, $attack['suit_id'], $attack['face']);
						break;
					}
					else
						continue;
				endforeach;
			}
			$this->session->set_userdata('table', $table);
			$player['first'] = $this->get_hand(1);
			$player['second'] = $this->get_hand(2);
			$player['table'] = $this->session->userdata('table');
			if($turn == 1)
				$this->load->view('trial/p2_def_view', $player);
			else
				$this->load->view('trial/p1_def_view', $player);
		} //end set_attack

		function set_defense($turn, $index)
		{
			$table = $this->session->userdata('table');
			$trump = $this->session->userdata('trump');
			$turn = intval($turn);
			$hand = $this->get_hand($turn);
			$suit = $trump['suit_id'];
			$defend = $hand[$index];
			$attack = end($table);
			$a_face = intval($attack['face']);
			$d_face = intval($attack['face']);
			if(($attack['suit_id'] != $suit) and ($defend['suit_id'] == $suit)) //a_suit is not trump suit and d_suit is trump suit
			{
				unset($hand[$index]);
				$hand = array_values($hand);
				array_push($table, $defend);
				$this->card_model->remove_from($turn, $attack['suit_id'], $attack['face']);
			}

			if(($attack['suit_id'] == $defend['suit_id']) and ($d_face > $a_face)) //if suits are equal
			{
				unset($hand[$index]);
				$hand = array_values($hand);
				array_push($table, $defend);
				$this->card_model->remove_from($turn, $attack['suit_id'], $attack['face']);	
			}
	
			$this->session->set_userdata('table', $table);
			$player['first'] = $this->get_hand(1);
			$player['second'] = $this->get_hand(2);
			$player['table'] = $this->session->user('table');
			if($turn == 1)
				$this->load->view('trial/p2_view', $player);
			else
				$this->load->view('trial/p1_view', $player);
		} //end set_defense

		function set_accept($player)
		{
			$table = $this->session->userdata('table');
			while(count($table) != 0) 
			{
				$card = array_pop($table);
				$this->card_model->set_hand($player, $card['suit_id'], $card['face']);
			}
			$this->session->set_userdata('table', $table);
		} //end set_accept 

		function end_attack($turn)
		{
			$table = $this->session->userdata('table');
			$turn = intval($turn);
			while(count($table) != 0)
				array_pop($table);
			$hand = get_hand($turn);
			$this->set_hand($hand, $turn);
			$player['first'] = $this->get_hand(1);
			$player['second'] = $this->get_hand(2);
			if($player == 1)
				$this->load->view('trial/p2_view', $player);
			else
				$this->load->view('trial/p1_view', $player);
		}
	}
?>